//
//  AticleheadCell.m
//  diaoyuren
//
//  Created by 钓鱼人 on 15/12/22.
//  Copyright © 2015年 game. All rights reserved.
//
#import "AticleheadCell.h"
#import "AppDelegate.h"
#import "TYAttributedLabel.h"
#import "UIImageView+WebCache.h"
#import "NSString+HtmlCharact.h"
#import "SDPhotoGroup.h"
#import "UIButton+WebCache.h"
#import "SDPhotoBrowser.h"
#import "ChenggeBtn.h"
#import "ImgListModel.h"
#import "NSString+ChangePPPPP.h"
#import "NSString+ClearPPPP.h"
#import "SDWebImageManager.h"
#import "Masonry.h"
#define AddsubView(view) [self.contentView addSubview:view]

@interface AticleheadCell ()<SDPhotoBrowserDelegate>

@property (nonatomic,assign) BOOL isZan;
@property (nonatomic,strong) TYAttributedLabel *contentLab;
@property (nonatomic,strong) SDPhotoBrowser *browser;
@property (nonatomic,assign) BOOL isLoad;
@property (nonatomic,strong) AppDelegate *app;
@end

@implementation AticleheadCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
          //底部点赞
        self.pramisNumb.layer.borderColor = RGBAS.CGColor;
        _pramisNumb = [self CreatBntWhithType:UIButtonTypeCustom AndBntImgName:@"btn-reply-green" WhithFont:0.05];
        _pramisNumb.layer.borderColor = mainColor.CGColor;
        _pramisNumb.layer.borderWidth = 1;
        radius(_pramisNumb, 5);
        
        [_pramisNumb setImage:[UIImage imageNamed:@"btn-reply-green"] forState:UIControlStateNormal];
        [_pramisNumb setTitleColor:mainColor forState:UIControlStateNormal];
        
        _pramisNumb.titleLabel.font = font(SCREEN_WIDTH * 0.055);
        _pramisNumb.titleLabel.textColor = mainColor;
        
        _pramisNumb.titleEdgeInsets = UIEdgeInsetsMake(5, 7, 0, 0);
        _pramisNumb.backgroundColor = [UIColor whiteColor];
        
        [_pramisNumb addTarget:self action:@selector(Zanclicklick:) forControlEvents:UIControlEventTouchUpInside];
        
        
        [_pramisNumb setImage:[UIImage imageNamed:@"btn-reply-green"] forState:UIControlStateSelected | UIControlStateHighlighted];
        [_pramisNumb setTitleColor:mainColor forState:UIControlStateSelected];
        [_pramisNumb setTitleColor:mainColor forState:UIControlStateSelected | UIControlStateHighlighted];

        
        [_pramisNumb setTitleColor:mainColor forState:UIControlStateNormal];
        [_pramisNumb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-25);
            make.centerX.equalTo(self.contentView.mas_centerX);
            make.height.equalTo(@40);
            make.width.equalTo(@90);
        }];

        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor whiteColor];
        
    }

    return self;
}

-(TYAttributedLabel *)contentLab{
    if (!_contentLab) {
        _contentLab = [[TYAttributedLabel alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 0)];
        
        [self.contentView addSubview:_contentLab];
        _contentLab.userInteractionEnabled = YES;
//        _contentLab.backgroundColor = [UIColor purpleColor];
    }
    return _contentLab;
}


-(void)Zanclicklick:(UIButton *)sender{

    if ([self.app.isLogin isEqualToString:@"YES"]) {
        
        if (!sender.selected) {
            sender.selected = YES;
      
            NSInteger count = sender.titleLabel.text.integerValue+1;
            
            [sender setTitle:[NSString stringWithFormat:@"%ld",(long)count] forState:UIControlStateNormal];
            
            [UIView animateWithDuration:0.5 animations:^{
                
                sender.imageView.transform = CGAffineTransformScale(sender.imageView.transform, 0.65, 0.65);
            } completion:^(BOOL finished) {
                
                [self.deleget PramClick];
                
            }];
            
        }
        
    }else{
        [self.deleget PramClick];
        
    }


}

/**
 * _zanBUtton代理方法  登录状态与未登录状态的
 */
-(void)LoginClick{
    self.isZan = YES;
    
    [self.deleget PramClick];
    
}
-(void)NoLoginClik{

    [self.deleget PramClick];
}

-(void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    self.contentLab.y = Height(self.time)+10;

}

-(void)Refresh{

    self.app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    if (self.isLoad) {
        return;
    }

    self.title.text = _detaiModel.title;
    
    if (!_detaiModel.writer) {
        self.Sources.text = @"钓鱼人";
    }else{
        self.Sources.text = _detaiModel.writer;
    }
    
    [self.time setTitle:_detaiModel.pubdate forState:UIControlStateNormal];
    [self.pramisNumb setTitle:_detaiModel.goodpost forState:UIControlStateNormal];

    
    /**
     * cell 虽然有复用机制  但是 Refresh 还是每次都会执行  且Model 并没有跟新 所以每次cell重新出现时赞的数量还是原来Model中的值
     *  点了赞后把 self.isZan 设置为 YES ,并把加1后的数传给 self.zanCount
     */
    if (self.isZan) {
        
        [self.pramisNumb setTitle:[NSString stringWithFormat:@"%ld",_detaiModel.goodpost.integerValue+1] forState:UIControlStateNormal];
        
    }else{
        
        [self.pramisNumb setTitle:_detaiModel.goodpost forState:UIControlStateNormal];
    }
    
    
    NSMutableArray * textArray = [NSMutableArray arrayWithArray:[[NSString ChangePPPP:_detaiModel.body] componentsSeparatedByString:@"</chengge>"]];
    NSInteger index = 0;
    
    for (NSString *text in textArray) {
        
            if ([text rangeOfString:@"<!--DY-IMG-"].length!=0){
                
                for (ImgListModel * imagemodel in self.imageAry) {
                    
                    if ([text rangeOfString:imagemodel.img_mark].length!=0) {
                        
                        CGFloat bili = (SCREEN_WIDTH-20)/imagemodel.width;
                        UIImageView   *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-20, imagemodel.height*bili)];
                        imageview.userInteractionEnabled = YES;
                        imageview.tag = [self.imageAry indexOfObject:imagemodel];
                        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ImageTapClick:)];
                        [imageview addGestureRecognizer:tap];
                        [imageview sd_setImageWithURL:[NSURL URLWithString:imagemodel.url]];
                        [_contentLab appendView:imageview];
                    }
                    
                }
            }else if ([text rangeOfString:@"<p>"].length!=0){
                
                if ([text rangeOfString:@"<strong>"].length!=0){
                    
                    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:[[text ClearhSimpStrong] filterresult]];
                    
                    [attributedString addAttributeFont:Boldfont(SCREEN_WIDTH*0.06)];
                    [self.contentLab appendTextAttributedString:attributedString];
                    
                }else{
                
                    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:[[text ClearPPP] filterresult]];
                    
                    [attributedString addAttributeFont:font(SCREEN_WIDTH*0.045)];
                    [self.contentLab setCharacterSpacing:3];
                    [self.contentLab appendTextAttributedString:attributedString];
                }

            }else if ([text rangeOfString:@"<h1>"].length!=0){
                
                NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:[[text Clearh1111] filterresult]];
                
                [attributedString addAttributeFont:font(SCREEN_WIDTH*0.075)];
                [self.contentLab appendTextAttributedString:attributedString];
                
            }else if ([text rangeOfString:@"<h2>"].length!=0){
                
                NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:[[text Clearh2222] filterresult]];
                
                [attributedString addAttributeFont:font(SCREEN_WIDTH*0.07)];
                [self.contentLab appendTextAttributedString:attributedString];
                
            }else if ([text rangeOfString:@"<h3>"].length!=0){
                
                NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:[[text Clearh3333] filterresult]];
                
                [attributedString addAttributeFont:font(SCREEN_WIDTH*0.065)];
                [self.contentLab appendTextAttributedString:attributedString];
                
            }else if ([text rangeOfString:@"<h4>"].length!=0){
                
                NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:[[text Clearh4444] filterresult]];
                
                [attributedString addAttributeFont:font(SCREEN_WIDTH*0.06)];
                [self.contentLab appendTextAttributedString:attributedString];
                
            }else if ([text rangeOfString:@"<h5>"].length!=0){
                
                NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:[[text Clearh5555] filterresult]];
                
                [attributedString addAttributeFont:font(SCREEN_WIDTH*0.055)];
                [self.contentLab appendTextAttributedString:attributedString];
                
            }else if ([text rangeOfString:@"<strong>"].length!=0){
                
                NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:[[text ClearhSimpStrong] filterresult]];
                
                [attributedString addAttributeFont:Boldfont(SCREEN_WIDTH*0.06)];
                [self.contentLab appendTextAttributedString:attributedString];
                
            }else{
            
                NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:[text filterresult]];
                [attributedString addAttributeFont:font(SCREEN_WIDTH*0.08)];
                [self.contentLab setCharacterSpacing:3];
                [self.contentLab appendTextAttributedString:attributedString];
            }
         [_contentLab appendText:@"\n"];
        index++;
    }
    
    CGFloat hhh = [self.contentLab getHeightWithWidth:SCREEN_WIDTH-20];
    self.contentLab.height = hhh-80;
    
//    NSLog(@"-----%f",hhh);
    self.cellHhhh =hhh+180;
    self.height = self.cellHhhh;
    self.isLoad = YES;
}



//标题
-(UILabel *)title{
    if (!_title) {
        _title = [self CrecLabel:_title WhithFont:0.07 AndColor:[UIColor blackColor] AndAlpha:0.9];
        [_title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(@10);
            make.right.equalTo(@-10);
        }];
    }

    return _title;
}

//来源
-(UILabel *)Sources{
    
    if (!_Sources) {
        _Sources = [self CrecLabel:_Sources WhithFont:0.04 AndColor:[UIColor grayColor] AndAlpha:0.4];
        [_Sources mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_title.mas_bottom).offset(10);
            make.left.equalTo(_title.mas_left);
        }];
    }

    return _Sources;
}

//时间
-(UIButton *)time{
    if (!_time) {
        _time = [self CreatBntWhithType:UIButtonTypeCustom AndBntImgName:@"clock" WhithFont:0.045];
        _time.titleEdgeInsets = UIEdgeInsetsMake(-1.5, 5, 0, -5);
        [_time setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_time mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_Sources.mas_right).offset(15);
            make.centerY.equalTo(_Sources.mas_centerY);
        }];

    }

    return _time;
}


-(UILabel *)CrecLabel:(UILabel *)label WhithFont:(CGFloat )font AndColor:(UIColor *)color AndAlpha:(CGFloat)alpha{
    
    label = [[UILabel alloc]init];
    label.font = font(SCREEN_WIDTH*font);
    label.tintColor = color;
    label.alpha = alpha;
    label.numberOfLines = 0;
    AddsubView(label);
    return label;
}
-(UIButton *)CreatBntWhithType:(UIButtonType)type AndBntImgName:(NSString *)string WhithFont:(CGFloat )font
{
    UIButton *bnt=[UIButton buttonWithType:type];
    
    [bnt setImage:[UIImage imageNamed:string] forState:UIControlStateNormal];
    bnt.titleLabel.font = font(SCREEN_WIDTH*font);
    AddsubView(bnt);
    return bnt;
}

#pragma mark - photobrowser代理方法
-(void)ImageTapClick:(UITapGestureRecognizer *)tap{
    
    _browser = [[SDPhotoBrowser alloc] init];
    _browser.sourceImagesContainerView = self.contentLab; // 原图的父控件
    _browser.imageCount = self.imageAry.count; // 图片总数
    _browser.delegate = self;
    
    _browser.alpha = 0;
    _browser.currentImageIndex = (int)tap.view.tag;
    [_browser show];
    
    [UIView animateWithDuration:0.8 animations:^{
        _browser.alpha = 1;
    }];
}



#pragma mark - photobrowser代理方法
// 返回临时占位图片（即原来的小图）
- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    return nil;//[self.subviews[ininin] currentImage];
}

// 返回高质量图片的url
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    
    ImgListModel *model = self.imageAry[index];
    NSString *urlStr = [model.url stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
    return [NSURL URLWithString:urlStr];
}

@end
